export default class WebSocketPackage {
    constructor(url) {
        this.wsUrl = ''
        this.setIntervalWesocketPush = null
    }
    createSocket() {
        Socket && Socket.close()
        if (!Socket) {
            console.log('建立websocket连接')
            Socket = new WebSocket(this.wsUrl)
            Socket.onopen = onopenWS
            Socket.onmessage = onmessageWS
            Socket.onerror = onerrorWS
            Socket.onclose = oncloseWS
        } else {
            console.log('websocket已连接')
        }
    };
    /**打开WS之后发送心跳 */
    onopenWS() {
        sendPing()
    };
    /**连接失败重连 */
    // onerrorWS /**打开WS之后发送心跳 */
    onopenWS() {
        sendPing()
    };
    /**连接失败重连 */
    onerrorWS() {
        Socket.close()
        clearInterval(setIntervalWesocketPush)
        console.log('连接失败重连中')
        if (Socket.readyState !== 3) {
            Socket = null
            createSocket()
        }
    };
    /**WS数据接收统一处理 */
    onmessageWS = e => {
        window.dispatchEvent(new CustomEvent('onmessageWS', {
            detail: {
                data: e.data
            }
        }))
    };
    /**
    发送数据但连接未建立时进行处理等待重发
    @param {any} message 需要发送的数据
    */
    connecting = message => {
        setTimeout(() => {
            if (Socket.readyState === 0) {
                connecting(message)
            } else {
                Socket.send(JSON.stringify(message))
            }
        }, 1000)
    };

    /**

    发送数据
    @param {any} message 需要发送的数据
    */
    sendWSPush(message) {
        if (Socket != null && Socket.readyState == 3) {
            Socket.close()
            createSocket()
        } else if (Socket.readyState = 1) {
            Socket.send(JSON.stringify(message))
        } else if (Socket.readyState = 0) {
            connecting(message)
        }
    };

    /*断开重连 */
    oncloseWS() {
        clearInterval(setIntervalWesocketPush)
        console.log('websocket已断开....正在尝试重连')
        if (Socket.readyState !== 2) {
            Socket = null
            createSocket()
        }
    };
    /*发送心跳

    @param {number} time 心跳间隔毫秒 默认5000
    @param {string} ping 心跳名称 默认字符串ping
    */
    sendPing(time = 5000, ping = 'ping') {
            clearInterval(setIntervalWesocketPush)
            Socket.send(ping)
            setIntervalWesocketPush = setInterval(() => {
                Socket.send(ping)
            }, time)
        }
        // {
        //     Socket.close()
        //     clearInterval(setIntervalWesocketPush)
        //     console.log('连接失败重连中')
        //     if (Socket.readyState !== 3) {
        //         Socket = null
        //         createSocket()
        //     }
        // };
        /**WS数据接收统一处理 */
    onmessageWS = e => {
        window.dispatchEvent(new CustomEvent('onmessageWS', {
            detail: {
                data: e.data
            }
        }))
    };
    /**
    
        发送数据但连接未建立时进行处理等待重发
        @param {any} message 需要发送的数据
        */
    connecting = message => {
        setTimeout(() => {
            if (Socket.readyState === 0) {
                connecting(message)
            } else {
                Socket.send(JSON.stringify(message))
            }
        }, 1000)
    };

    /**
    
    发送数据
    @param {any} message 需要发送的数据
    */
    sendWSPush = message => {
        if (Socket !== null && Socket.readyState == 3) {
            Socket.close()
            createSocket()
        } else if (Socket.readyState = 1) {
            Socket.send(JSON.stringify(message))
        } else if (Socket.readyState = 0) {
            connecting(message)
        }
    };

    /*断开重连 */
    oncloseWS() {
        clearInterval(setIntervalWesocketPush)
        console.log('websocket已断开....正在尝试重连')
        if (Socket.readyState !== 2) {
            Socket = null
            createSocket()
        }
    };
    /*发送心跳
    
        @param {number} time 心跳间隔毫秒 默认5000
        @param {string} ping 心跳名称 默认字符串ping
        */
    sendPing(time = 5000, ping = 'ping') {
        clearInterval(setIntervalWesocketPush)
        Socket.send(ping)
        setIntervalWesocketPush = setInterval(() => {
            Socket.send(ping)
        }, time)
    };

}